package org.openurp.edu.student.info.web.action

import org.beangle.webmvc.entity.action.RestfulAction
import org.openurp.edu.student.info.model.Student
import org.openurp.edu.student.info.web.helper.ExportExcelUtils
import org.beangle.data.dao.OqlBuilder
import org.beangle.commons.lang.ClassLoaders
import java.io.OutputStream
import org.beangle.webmvc.api.view.View
import org.beangle.commons.collection.Order
import org.beangle.webmvc.api.action.ServletSupport
import org.beangle.webmvc.api.annotation.response
import org.beangle.webmvc.api.view.Status

/**
 * @author xinzhou
 */
/**
 * 其他教师查看学生信息
 */
class TeacherAction extends RestfulAction[Student] with ServletSupport {

  protected override def getQueryBuilder(): OqlBuilder[Student] = {
    val builder = OqlBuilder.from(classOf[Student], "student")
    populateConditions(builder)
    get("name") foreach { name =>
      builder.where("lower(student.name) like :name", "%" + name.toLowerCase + "%")
    }
    builder.orderBy(get(Order.OrderStr).orNull).limit(getPageLimit)
  }

  def exportexcel(): View = {
    val builder = this.getQueryBuilder()
    builder.limit(null)
    val list = collection.JavaConversions.asJavaCollection(entityDao.search(builder))
    //查出学生信息并放到map中
    val beans = new java.util.HashMap[String, Any]
    beans.put("list", list);
    //获得模板路径
    val path = ClassLoaders.getResourceAsStream("student_information.xls");
    //准备输出流
    var os: OutputStream = null;
    response.setContentType("application/x-excel");
    response.setHeader("Content-Disposition", "attachment;filename=studentInfo.xls");
    os = response.getOutputStream();
    ExportExcelUtils.excprtStudentExcel(beans, path, os);
    Status.Ok
  }

}